<!--
 * @Description: 设备管理
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:02:33
-->
<template>
  <div>
    <el-row>
      <el-col :span="20">
        <el-form :inline="true" :model="formSearch" class="demo-form-inline">
          <el-form-item>
            <el-select clearable v-model="formSearch.online_status" placeholder="请选择设备状态">
              <el-option label="在线" :value="1"></el-option>
              <el-option label="离线" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select clearable v-model="formSearch.bind_status" placeholder="请选择设备绑定状态">
              <el-option label="未绑定" :value="0"></el-option>
              <el-option label="已绑定" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-form-item>
              <el-input clearable v-model="formSearch.name" placeholder="请输入设备名称"></el-input>
            </el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="toSearch">搜索</el-button>

          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="4" style="text-align: right;">
        <el-button type="primary" @click="toAdd">录入</el-button>
      </el-col>
    </el-row>
    <!-- 录入抽屉 -->
    <el-drawer @close="closeAdd" size="50%" title="录入设备信息" :visible.sync="addVisible" direction="rtl"
      custom-class="demo-drawer">
      <div class="demo-drawer__content" style="padding: 10px 15px 10px 0;border-top: 1px solid #ededed;">
        <el-form :model="addForm" :rules="rules" ref="addForm">
          <el-form-item label="编号" :label-width="formLabelWidth" prop="serial_number">
            <el-input placeholder="请输入设备编号" clearable v-model="addForm.serial_number" autocomplete="off"
              style="width: 100%;"></el-input>
          </el-form-item>
          <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
            <el-input placeholder="请输入设备名称" clearable v-model="addForm.name" autocomplete="off"
              style="width: 100%;"></el-input>
          </el-form-item>
          <div class="demo-drawer__footer" style="text-align: center; margin-top: 20px;">
            <el-button @click="addVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitAdd">确 定</el-button>
          </div>
        </el-form>
      </div>
    </el-drawer>

    <!-- 第2行表格 -->
    <div class="table">
      <el-table :data="empData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="60" align="center">
        </el-table-column>
        <el-table-column prop="serial_number" label="编号" width="100" align="center">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="700">
        </el-table-column>
        <el-table-column prop="online_status" label="在线状态" align="center">
          <template v-slot="scope">
            {{ scope.row.online_status == 0 ? '离线' : '在线' }}
          </template>
        </el-table-column>
        <el-table-column prop="bind_status" label="绑定状态" align="center">
          <template v-slot="scope">
            {{ scope.row.bind_status == 0 ? '未绑定' : '已绑定' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-link style="font-size: 12px;" :underline="false"
              type="primary" @click="toEdit(scope.row)">修改</el-link>
            <el-link :disabled="scope.row.bind_status !== 0" style="margin-left: 5px;font-size: 12px;"
              :underline="false" type="danger" @click="toDelete(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>

    </div>

    <!-- 修改抽屉 -->
    <el-drawer @close="close" size="50%" :title="title" :visible.sync="visible" direction="rtl"
      custom-class="demo-drawer" ref="drawer">
      <div class="demo-drawer__content" style="padding: 10px 15px 10px 0;border-top: 1px solid #ededed;">
        <el-form :model="form" :rules="rules" ref="myForm">


          <el-form-item label="编号" :label-width="formLabelWidth" prop="serial_number">
            <el-input placeholder="请输入设备编号" clearable v-model="form.serial_number" autocomplete="off"
              style="width: 100%;"></el-input>
          </el-form-item>


          <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
            <el-input placeholder="请输入设备名称" clearable v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>


          <div class="demo-drawer__footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
        </el-form>
      </div>

    </el-drawer>
  </div>

</template>
<script>
import { get, del, postJSON } from '@/utils/request'
export default {
  data() {
    return {
      //录入相关数据
      addVisible: false,
      addForm: {
        serial_number: '',
        name: ''
      },
      //搜索表单
      formSearch: {},
      //表格数据源
      empData: [],
      params: {
        page: 1,
        pageSize: 10
      },
      //修改表单
      visible: false,
      title: '修改设备信息',
      form: {
        serial_number: '',
        name: ''
      },
      formLabelWidth: '80px',
      rules: {
        serial_number: [
          { required: true, message: '请输入设备编号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入设备名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {

    //去查询
    toSearch() {
      this.params.page = 1;
      this.getEmpData();
    },

    //获取设备数据
    async getEmpData() {
      //判断条件存不存在 存在根据条件查询 不存在就删除条件
      let temp = {
        ...this.params,
        ...this.formSearch
      }
      //   for (let key in temp){
      //   if(!temp[key]){
      //     delete temp[key]
      //   }
      // }
      let res = await get('/device/pageQuery', temp);
      console.log(res, '获取响应');
      this.empData = res.data.list;
    },
    // 打开修改弹窗
    toEdit(row) {
      this.visible = true;
      this.form = {
        ...row
      };
    },
    // 关闭弹窗
    close() {
      this.$refs.myForm.resetFields();
    },
    // 提交修改表单
    async submitForm() {
      try {
        await this.$refs.myForm.validate(async (valid) => {
          if (valid) {
            await postJSON('/device/saveOrUpdate', this.form);
            this.$message.success('修改成功');
            this.visible = false;
            this.getEmpData(); // 刷新数据
          }
        });
      } catch (error) {
        this.$message.error('修改失败');
        console.error('修改失败:', error);
      }
    },

    // 添加设备
    toAdd() {
      this.addVisible = true
      this.addForm = {
        serial_number: '',
        name: ''
      }
      this.$nextTick(() => {
        this.$refs.addForm?.clearValidate()
      })
    },
    // 提交录入表单
    async submitAdd() {
      try {
        await this.$refs.addForm.validate(async (valid) => {
          if (valid) {
            await postJSON('/device/saveOrUpdate', this.addForm);
            this.$message.success('录入成功');
            this.addVisible = false;
            this.getEmpData(); // 刷新数据
          }
        });
      } catch (error) {
        this.$message.error('录入失败');
        console.error('录入失败:', error);
      }
    },
    // 关闭录入抽屉
    closeAdd() {
      this.$refs.addForm?.resetFields()
    },
    // 删除设备
    async toDelete(id) {
      try {
        await this.$confirm('此操作将永久删除该设备, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });

        await del('/device/deleteById', { id });
        this.$message.success('删除成功');
        this.getEmpData(); // 刷新数据
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败');
          console.error('删除失败:', error);
        }
      }
    }
  },
  created() {
    this.getEmpData();
  }
}
</script>
<style lang="scss" scoped>
.demo-drawer__footer {
  border-top: 1px solid #ededed;
  padding-top: 10px;
  //固定定位
  position: fixed;
  bottom: 50px;
  width: 50%;
  text-align: center;
}
</style>
